import { Component, OnInit } from '@angular/core';
import { AfterViewInit, ViewChild } from '@angular/core';
import { NzFormatEmitEvent, NzTreeNodeOptions } from 'ng-zorro-antd/core';
import { NzTreeComponent } from 'ng-zorro-antd/tree';


@Component({
  selector: 'app-tree2',
  templateUrl: './tree2.component.html',
  styleUrls: ['./tree2.component.css']
})
export class Tree2Component implements OnInit {


 public children2:any=  [
    { title: "你好", key: "11", isLeaf: true },
    { title: "你好", key: "11", isLeaf: true }
  ]

  @ViewChild('nzTreeComponent', { static: false }) nzTreeComponent: NzTreeComponent;
  defaultCheckedKeys = ['10020'];
  defaultSelectedKeys = ['10010'];
  defaultExpandedKeys = ['100', '1001'];

  nodes: NzTreeNodeOptions[] = [
    {
      title: 'parent 1',
      key: '100',
      children: [
        {
          title: 'parent 1-0',
          key: '1001',
          disabled: true,
          children: [
            { title: 'leaf 1-0-0', key: '10010', disableCheckbox: true, isLeaf: true },
            { title: 'leaf 1-0-1', key: '10011', isLeaf: true }
          ]
        },
        {
          title: 'parent 1-1',
          key: '1002',
          children: [
            { title: 'leaf 1-1-0', key: '10020', isLeaf: true },
            { title: 'leaf 1-1-1', key: '10021', isLeaf: true }
          ]
        }
      ]
    }
  ];


  constructor() { }

  ngOnInit() {
  }

  nzClick(event: NzFormatEmitEvent): void {
    console.log(event);
  }

  nzCheck(event: NzFormatEmitEvent): void {
    console.log(event);
  }

  // nzSelectedKeys change
  nzSelect(keys: string[]): void {
    console.log(keys, this.nzTreeComponent.getSelectedNodeList());
  }

  ngAfterViewInit(): void {
    // get node by key: '10011'
    console.log(this.nzTreeComponent.getTreeNodeByKey('10011'));
    // use tree methods
    console.log(
      // 获取组件 NzTreeNode 节点
      this.nzTreeComponent.getTreeNodes(),
      // 获取组件 checkBox 被点击选中的节点
      this.nzTreeComponent.getCheckedNodeList(),
      // 	获取组件被选中的节点
      this.nzTreeComponent.getSelectedNodeList(),
      // 获取组件展开状态节点
      this.nzTreeComponent.getExpandedNodeList()
    );
  }

  getdata() {
    console.log("获取组件 NzTreeNode 节点")
    console.log(this.nzTreeComponent.getTreeNodes())
    console.log("获取组件 checkBox 被点击选中的节点")
    console.log(this.nzTreeComponent.getCheckedNodeList())
    console.log("获取组件被选中的节点")
    console.log(this.nzTreeComponent.getSelectedNodeList())
    console.log("获取组件展开状态节点")
    console.log(this.nzTreeComponent.getExpandedNodeList())
    console.log("获取组搜索匹配到的节点")
    console.log(this.nzTreeComponent.getMatchedNodeList())
    // 按 key 获取 NzTreeNode 节点
    console.log("按 key 获取 NzTreeNode 节点")
    console.log(this.nzTreeComponent.getTreeNodeByKey("1001"))
    // var a = this.nzTreeComponent.getTreeNodes()
    // for (var i = 0; i < a.length; i++) {
    //   console.log(a[i])
    //   a[i].addChildren(this.children2)
    // }

    var q=this.nzTreeComponent.getTreeNodeByKey("100")
    q.addChildren(this.children2)

  }

}
